package com.example.child_protection.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.child_protection.dao.AssessDao;
import com.example.child_protection.dao.ChildAssessDao;
import com.example.child_protection.dao.ChildAssessValuesDao;
import com.example.child_protection.dto.info.AssessDTO;
import com.example.child_protection.dto.info.ChildAssessDTO;
import com.example.child_protection.dto.request.SimpleIdRequestDTO;
import com.example.child_protection.dto.request.find.ChildAssessRequestDTO;
import com.example.child_protection.dto.response.ChildAssessListResponseDTO;
import com.example.child_protection.dto.response.ChildAssessResponseDTO;
import com.example.child_protection.enums.BooleanEnums;
import com.example.child_protection.enums.ServiceActionEnums;
import com.example.child_protection.model.AssessModel;
import com.example.child_protection.model.ChildAssessModel;
import com.example.child_protection.model.ChildAssessValuesModel;
import com.example.child_protection.service.ChildAssessQueryService;
import com.example.child_protection.service.base.MyBaseService;
import com.example.child_protection.service.base.MyServiceExecutor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 儿童高危信息查询service实现层
 *
 * @author Biz
 * @date 2018-11-2
 */
@Component
public class ChildAssessQueryServiceImpl extends MyBaseService implements ChildAssessQueryService {

    @Autowired
    public ChildAssessDao childAssessDao;

    @Autowired
    public AssessDao assessDao;

    @Autowired
    public ChildAssessValuesDao childAssessValuesDao;

    /**
     * id查询
     *
     * @param requestDTO
     * @return
     */
    public ChildAssessResponseDTO findId(SimpleIdRequestDTO requestDTO) {
        return this.service(ServiceActionEnums.CHILD_ASSESS_FIND_ID, requestDTO, ChildAssessResponseDTO.class, new MyServiceExecutor<SimpleIdRequestDTO, ChildAssessResponseDTO>() {
            @Override
            protected ChildAssessResponseDTO myprocess(SimpleIdRequestDTO requestDTO) throws Exception {
                ChildAssessResponseDTO responseDTO = new ChildAssessResponseDTO();
                ChildAssessModel childAssessModel = childAssessDao.findId(requestDTO.getId());
                ChildAssessDTO childAssess = new ChildAssessDTO();
                BeanUtils.copyProperties(childAssessModel, childAssess);
                AssessModel assessModel = new AssessModel();
                assessModel.setAssessId(requestDTO.getId());
                List<AssessModel> cAssesss = assessDao.findList(assessModel);
                childAssess.setValues(JSONObject.parseArray(JSONArray.toJSONString(cAssesss), AssessDTO.class));
                responseDTO.setInfo(childAssess);
                responseDTO.successfully();
                return responseDTO;
            }
        });
    }

    /**
     * 列表查询
     *
     * @param requestDTO
     * @return
     */
    public ChildAssessListResponseDTO findList(ChildAssessRequestDTO requestDTO) {
        return this.service(ServiceActionEnums.CHILD_ASSESS_FIND_LIST, requestDTO, ChildAssessListResponseDTO.class, new MyServiceExecutor<ChildAssessRequestDTO, ChildAssessListResponseDTO>() {
            @Override
            protected ChildAssessListResponseDTO myprocess(ChildAssessRequestDTO requestDTO) throws Exception {
                ChildAssessListResponseDTO responseDTO = new ChildAssessListResponseDTO();
                ChildAssessModel req = new ChildAssessModel();
                BeanUtils.copyProperties(requestDTO, req);
                List<ChildAssessModel> lists = childAssessDao.findPage(req);
                List<ChildAssessDTO> assesss = JSONObject.parseArray(JSONArray.toJSONString(lists), ChildAssessDTO.class);
                AssessModel assessModel = new AssessModel();
                List<AssessModel> cAssesss = null;
                if(requestDTO.getIsOnlyRoot().equals(BooleanEnums.F)) {
                    for (ChildAssessDTO as : assesss) {
                        assessModel = new AssessModel();
                        cAssesss = new ArrayList<>();
                        assessModel.setAssessId(as.getId());
                        cAssesss = assessDao.findList(assessModel);
                        as.setValues(JSONObject.parseArray(JSONArray.toJSONString(cAssesss), AssessDTO.class));
                    }
                }
                responseDTO.setInfos(assesss);
                responseDTO.successfully();
                return responseDTO;
            }
        });
    }
}
